﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SATF.DB;

namespace SATF.BI
{
    public class BIMachine : BaseItem
    {
        #region 所有成员属性

        private UInt64 m_group_id = 0;
        public UInt64 Group_ID
        {
            get { return m_group_id; }
            set
            {
                m_group_id = value;
                OnPropertyChanged("Group_ID");
            }
        }

        private BIUserGroup m_userGroup = null;
        public BIUserGroup UserGroup
        {
            get { return m_userGroup; }
            set
            {
                m_userGroup = value;
                OnPropertyChanged("UserGroup");
            }
        }

        private UInt64 m_user_id = 0;
        public UInt64 User_ID
        {
            get { return m_user_id; }
            set
            {
                m_user_id = value;
                OnPropertyChanged("User_ID");
            }
        }

        private string m_ip = "";
        public string IP
        {
            get { return m_ip; }
            set
            {
                m_ip = value;
                OnPropertyChanged("IP");
            }
        }

        private int m_ie_type = -1;
        public int IE_Type
        {
            get { return m_ie_type; }
            set
            {
                m_ie_type = value;
                OnPropertyChanged("IE_Type");
            }
        }

        private string m_ie_type_str = "";
        public string IE_Type_Str
        {
            get { return m_ie_type_str; }
            set
            {
                m_ie_type_str = value;
                OnPropertyChanged("IE_Type_Str");
            }
        }

        private int m_os_type = -1;
        public int OS_Type
        {
            get { return m_os_type; }
            set
            {
                m_os_type = value;
                OnPropertyChanged("OS_Type");
            }
        }

        private string m_os_type_str = "";
        public string OS_Type_Str
        {
            get { return m_os_type_str; }
            set
            {
                m_os_type_str = value;
                OnPropertyChanged("OS_Type_Str");
            }
        }

        private string m_desc = "";
        public string Desc
        {
            get { return m_desc; }
            set
            {
                m_desc = value;
                OnPropertyChanged("Desc");
            }
        }


        private UInt64 m_role = 0;
        public UInt64 Role
        {
            get { return m_role; }
            set
            {
                m_role = value;
                OnPropertyChanged("Role");
            }
        }

        private int m_state = 0;
        public int State
        {
            get { return m_state; }
            set
            {
                m_state = value;
                OnPropertyChanged("State");
            }
        }

        private string m_state_str = "";
        public string State_Str
        {
            get { return m_state_str; }
            set
            {
                m_state_str = value;
                OnPropertyChanged("State_Str");
            }
        }

        private int m_isdeleted = 0;
        public int Isdeleted
        {
            get { return m_isdeleted; }
            set
            {
                m_isdeleted = value;
                OnPropertyChanged("Isdeleted");
            }
        }

        private BIUser m_user = null;
        public BIUser User
        {
            get { return m_user; }
            set
            {
                m_user = value;
                OnPropertyChanged("User");
            }
        }

        private string m_userName = "";
        public string UserName
        {
            get { return m_userName; }
            set
            {
                m_userName = value;
                OnPropertyChanged("UserName");
            }
        }

        private string m_groupName = "";
        public string GroupName
        {
            get { return m_groupName; }
            set
            {
                m_groupName = value;
                OnPropertyChanged("GroupName");
            }
        }

        private bool m_enabled = true;
        public bool Enabled
        {
            get { return m_enabled; }
            set
            {
                m_enabled = value;
                OnPropertyChanged("Enabled");
            }
        }


        #endregion


        #region 构造方法

        public BIMachine()
            : base()
        {

        }

        public BIMachine(UInt64 id)
            : base(id)
        {

        }

        public BIMachine(string name)
            : base(name)
        {

        }

        public BIMachine(DataRow dataRow)
            : base(dataRow)
        {

        }

        #endregion

        
        #region 基类方法

        public override void SetTableName()
        {
            this.TableName = "machine";
        }

        public override string GetChildTable()
        {
            return "";
        }

        public override void Fill(System.Data.DataRow dataRow)
        {
            ID = DBConvert.ToUInt64(dataRow, "ID");
            Name = DBConvert.ToString(dataRow, "Name");
            IP = DBConvert.ToString(dataRow, "IP");
            User_ID = DBConvert.ToUInt64(dataRow, "User_ID");
            User = new BIUser(User_ID);
            if (User != null)
            {
                UserName = User.Name;
            }
            Group_ID = DBConvert.ToUInt64(dataRow, "Group_ID");
            UserGroup = new BIUserGroup(Group_ID);
            GroupName = UserGroup.Name;
            IE_Type = DBConvert.ToInt32(dataRow, "IE_Type");
            IE_Type_Str = BIConverters.IEType(IE_Type);
            OS_Type = DBConvert.ToInt32(dataRow, "OS_Type");
            OS_Type_Str = BIConverters.OSType(OS_Type);
            Desc = DBConvert.ToString(dataRow, "Desc");
            //权限直接访问组获取
            //Role = DBConvert.ToUInt64(dataRow, "Role");
            Role = UserGroup.Group_Role;
            State = DBConvert.ToInt32(dataRow, "State");
            State_Str = BIConverters.TestMachineState(State);
            IsDeleted = DBConvert.ToInt32(dataRow, "IsDeleted");

            this.ContentDataRow = dataRow;
        }

        public override void UpdateContentDataRow()
        {
            this.ContentDataRow["Name"] = Name;
            this.ContentDataRow["IP"] = IP;
            this.ContentDataRow["User_ID"] = User_ID;
            this.ContentDataRow["Group_ID"] = Group_ID;
            this.ContentDataRow["IE_Type"] = IE_Type;
            this.ContentDataRow["OS_Type"] = OS_Type;
            this.ContentDataRow["Desc"] = Desc;
            this.ContentDataRow["Role"] = Role;
            this.ContentDataRow["State"] = State;
            this.ContentDataRow["IsDeleted"] = IsDeleted;

            this.ContentDataRow = this.ContentDataRow;
        }

        #endregion


        #region 子类方法

        private bool IsMachineEnabled()
        {
            if (this.UserName == "")
            {
                return this.Role >= this.UserGroup.Group_Role;
            }
            else
            {
                //return this.UserName == this.u
                return true;
            }
        }

        #endregion


        #region 一些查询方法

        //需要考虑权限问题
        public static DataTable GetAllGroupID()
        {
            string sqlStr = "select distinct group_id from machine";
            return DBFactory.ExecuteQuery(sqlStr);
        }

        public static DataTable GetAllIEType()
        {
            string sqlStr = "select distinct ie_type from machine";
            return DBFactory.ExecuteQuery(sqlStr);
        }

        public static DataTable GetAllOSType()
        {
            string sqlStr = "select distinct os_type from machine";
            return DBFactory.ExecuteQuery(sqlStr);
        }

        public static DataTable GetAllRole()
        {
            string sqlStr = "select distinct role from machine";
            return DBFactory.ExecuteQuery(sqlStr);
        }

        public static DataTable GetOSTypeByGroupID(UInt64 groupID)
        {
            string sqlStr = string.Format("select distinct os_type from machine where group_id like {0}",
                groupID.ToString());
            return DBFactory.ExecuteQuery(sqlStr);
        }

        public static DataTable GetIETypeByGroupID(int groupID)
        {
            string sqlStr = string.Format("select distinct ie_type from machine where group_id like {0}",
                groupID.ToString());
            return DBFactory.ExecuteQuery(sqlStr);
        }

        public override IList<BaseItem> GetChildren()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}
